﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public  class UQualityQuestionnaireDAL
    {
        public static int Add(UQualityQuestionnaireInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_Add",obj.MachineType,obj.SerialNumber,obj.CustomerName,obj.WorkAddress,obj.IntoConditi,obj.WorkTime,obj.IntoSpecial,obj.IntoDate,obj.CustomerMobile,obj.AgentName,obj.SurveyDate,obj.UQTIDS);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int UQQID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_Delete", UQQID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetByUQQID(int UQQID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_GetByUQQID", UQQID);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTable GetPagination(string SerialNumber, string CustomerName, string AgentName, string SurveyDateFrom, string SurveyDateTo, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_GetPagination");
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String,SerialNumber);
            DB.AddInParameter(dbCommand, "CustomerName", DbType.String,CustomerName);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String,AgentName);
            DB.AddInParameter(dbCommand, "SurveyDateFrom", DbType.String,SurveyDateFrom);
            DB.AddInParameter(dbCommand, "SurveyDateTo", DbType.String,SurveyDateTo);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
				            DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(UQualityQuestionnaireInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_Update",obj.UQQID,obj.MachineType,obj.SerialNumber,obj.CustomerName,obj.WorkAddress,obj.IntoConditi,obj.WorkTime,obj.IntoSpecial,obj.IntoDate,obj.CustomerMobile,obj.AgentName,obj.SurveyDate,obj.UQTIDS);
            try
            {
                return Convert.ToInt32(DB.ExecuteScalar(dbCommand)) > 0;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static IDataReader UQualityTemplete_GetALL()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityTemplete_GetALL");
            return DB.ExecuteReader(dbCommand);
	    }

        public static IDataReader UQualityList_GetByUQQID(int UQQID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityList_GetByUQQID", UQQID);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UQualityQuestionnaire_GetReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
    }
}
